package com.itcast.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itcast.constant.MessageConstant;
import com.itcast.dao.MemberDao;
import com.itcast.dao.OrderDao;
import com.itcast.dao.OrderSettingDao;
import com.itcast.entity.PageResult;
import com.itcast.entity.QueryPageBean;
import com.itcast.entity.Result;
import com.itcast.pojo.Member;
import com.itcast.pojo.Order;
import com.itcast.pojo.OrderSetting;
import com.itcast.service.OrderService;
import com.itcast.utils.DateUtils;
import com.itcast.vo.OrderSettingListVO;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @className: OrderServiceImpl
 * @description: TODO 类描述
 * @author: Jng
 * @date: 2022/4/14
 **/

@Service(interfaceClass = OrderService.class)
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderSettingDao orderSettingDao;
    @Autowired
    MemberDao memberDao;
    @Autowired
    OrderDao orderDao;
    @Override
    public Result order(Map map) throws Exception {
        //1.检查用户是所预约的日期否进行了预约设置，如果没有则不能预约
        String orderDate = (String) map.get("orderDate");
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(DateUtils.parseString2Date(orderDate));
        if (orderSetting==null){
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //2.检查用户预约日期的人数是否已满，如果已满就不能提交反之亦然
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if (reservations>=number){
            return new Result(false,MessageConstant.ORDER_FULL);
        }

        //3.检查用户是否在同一天预约了同一个项目，如果有则不能预约
        String telephone = (String) map.get("telephone");
        Member member=memberDao.findByTelephone(telephone);
        if(member!=null){
            Integer id = member.getId();
            Date date=DateUtils.parseString2Date(orderDate);
            Integer setmealId = Integer.parseInt((String)map.get("setmealId"));
            Order order=new Order(id,date,setmealId);
            List<Order> list =orderDao.findByCondition(order);
            if (list!=null&&list.size()>0){
                //已经预约了该项套餐，无法重复预约
                return new Result(false,MessageConstant.HAS_ORDERED);
            }
        }
        else {
            //4.检查用户是否位会员，如果是会员则直接完成预约，非会员则是先完成注册
            member=new Member();
            member.setIdCard((String) map.get("idCard"));
            member.setPhoneNumber(telephone);
            member.setRegTime(new Date());
            member.setName((String)map.get("name"));
            member.setSex((String)map.get("sex"));
            memberDao.add(member);
        }

        //5.预约成功，更新表orderSetting的reservations字段
        Order order=new Order();
        order.setOrderDate(DateUtils.parseString2Date(orderDate));
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setOrderType((String)map.get("orderType"));
        order.setMemberId(member.getId());
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));
        orderDao.add(order);

        orderSetting.setReservations(orderSetting.getReservations()+1);
        orderSettingDao.editReservationsByOrderDate(orderSetting);
        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
    }

    @Override
    public Map findById(Integer id) throws Exception {
        Map detail = orderDao.findById4Detail(id);
        if (detail!=null){
            Date orderDate = (Date) detail.get("orderDate");
            detail.put("orderDate", DateUtils.parseDate2String(orderDate));
        }
        return detail;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //当前页
        Integer currentPage = queryPageBean.getCurrentPage();
        // 每页大小
        Integer pageSize = queryPageBean.getPageSize();
        // 查询条件
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);
        Page<OrderSettingListVO> lists=orderDao.findAll(queryString);
        if (lists.getTotal()<=0){
            return new PageResult(false,MessageConstant.QUERY_RESERVATION_INFORMATION_MANAGEMENT_FAIL);
        }
        return new PageResult(true,lists.getTotal(),lists.getResult());
    }
}
